package com.hc.springtools.SpElUtils;

import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

/**
 * @Author: huangcheng
 * @CreateTime: 2024-07-25  10:19
 * @Description:
 * @Version: 1.0
 */
public class StudyTest {

    public static void main(String[] args) {
        Student student = new Student("张三", 11);
        ExpressionParser parser = new SpelExpressionParser();
        Expression exp = parser.parseExpression("#student.name");
        StandardEvaluationContext context = new StandardEvaluationContext();
        // 输入多个数据源
        context.setVariable("student", student);
        context.setVariable("name", "测试");

        String name = exp.getValue(context, String.class);
        // 假如表达式是#name输出结果是 测试
        // 假如表达式是#student.name输出结果是 张三
        System.out.println(name);
    }
}
